    //
//  RecommondViewController.swift
//  DouYuZB
//
//  Created by James on 2017/10/15.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit

private let kItemMargin : CGFloat = 10
private let kNormalItemW = (kScreenW - 3 * kItemMargin) / 2
private let kNormalItemH = kNormalItemW * 3 / 4
private let kPrettyItemH = kNormalItemW * 4 / 3


private let kNormalCellID = "kNormalCellID"
private let kHeaderView = "kHeaderView"
private let kPrettyCell = "kPrettyCell"
private let kCycleViewH = kScreenW * 3 / 8
private let kHeadViewH = CGFloat(50.0)
private let kGameViewH = CGFloat(90.0)

class RecommondViewController: UIViewController {
    // MARK: - 懒加载
    lazy var collectionView: UICollectionView = {[weak self] in
        
        let layOut = UICollectionViewFlowLayout()
        
        layOut.minimumLineSpacing = 10
        layOut.minimumInteritemSpacing = kItemMargin
        layOut.scrollDirection = .vertical
        layOut.headerReferenceSize = CGSize(width: kScreenW, height: kHeadViewH)
        layOut.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        
        let collectionView = UICollectionView(frame: (self?.view.bounds)!, collectionViewLayout: layOut)
        collectionView.dataSource = self
        collectionView.delegate = self
        
        collectionView.register(UINib.init(nibName: "CollectionNormalCell", bundle: nil), forCellWithReuseIdentifier: kNormalCellID)
        
        collectionView.register(UINib.init(nibName: "CollectionPrettyCell", bundle: nil), forCellWithReuseIdentifier: kPrettyCell)
        
        collectionView.register(UINib.init(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderView)
        
        collectionView.backgroundColor = UIColor.white
        collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        
        if #available(iOS 11,*){
            collectionView.contentInsetAdjustmentBehavior = .never
        }

        return collectionView
    }()
    
    lazy var recommondViewModel:RecommendViewModel = RecommendViewModel()
   
    lazy var cycleView : RecommendCycleView = {
        let cycleView = RecommendCycleView.recommendCycleView()
        cycleView.frame = CGRect(x: 0, y: -kCycleViewH-kGameViewH, width: kScreenW, height: kCycleViewH)
        return cycleView
    }()
    
    lazy var gameView: RecommondGameView  = {
        let view = RecommondGameView.recommondGameView()
        view.frame = CGRect(x: 0, y: cycleView.frame.maxY, width: kScreenW, height: kGameViewH)
        return view;
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.autoresizesSubviews = true
        setupUI()
        
        recommondViewModel.requestData {[weak self] in
            self?.collectionView.reloadData()
            //传数据给gameView
            self?.gameView.anchorGroup = self?.recommondViewModel.anchorGroup
        }
        
        recommondViewModel.requestCycleData {[weak self] in
            self?.cycleView.cycleModels = self?.recommondViewModel.cycleModels
        }
    }
    
}   

extension RecommondViewController {
    private func setupUI()  {
        view.addSubview(collectionView)
        //把轮播试图加到 collectionView
        collectionView.addSubview(cycleView)
        
        collectionView.addSubview(gameView)
        
        //设置内边距
        collectionView.contentInset = UIEdgeInsetsMake(kCycleViewH + kGameViewH, 0, 0, 0)
    }
}

extension RecommondViewController : UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return recommondViewModel.anchorGroup.count
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 1{
            return CGSize(width: kNormalItemW, height: kPrettyItemH)
        }else{
            return CGSize(width: kNormalItemW, height: kNormalItemH)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let group = recommondViewModel.anchorGroup[section]
        
        return group.anchors.count
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        //1.取出headerView
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeaderView, for: indexPath) as! CollectionHeaderView
        
        let group = recommondViewModel.anchorGroup[indexPath.section]
        
        headerView.group = group
        
        return headerView
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let group = recommondViewModel.anchorGroup[indexPath.section]
        let anchor = group.anchors[indexPath.item]
        
        var cell:CollectionBaseCell!
        if indexPath.section == 1 {//颜值的cell
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: kPrettyCell, for: indexPath) as! CollectionPrettyCell
        }else{
           cell = collectionView.dequeueReusableCell(withReuseIdentifier: kNormalCellID, for: indexPath) as! CollectionNormalCell
        }
        
        cell.anchor = anchor
        return cell
    }
}
